"""This test suite contains 2 passing tests and 2 failing tests."""
import time
import random
import string
import pytest
from seleniumbase import BaseCase
import tests_case  
BaseCase.main(__name__, __file__)


class MyTestSuite(BaseCase):
    def setUp(self):
        super().setUp()
        self.maximize_window()
    def test_search(self):
       tests_case.test_login(self)
       self.wait_for_element_visible('//div[@class="el-sub-menu__title"]//span[text()="考试"]',timeout=30)
       self.click('//div[@class="el-sub-menu__title"]//span[text()="考试"]')
       self.click("span:contains('题库管理')")
       
       self.click("button:contains('添加试题')")
      
       # 定位aria-label="添加试题"的对话框
       dialog_selector = '//div[@aria-label="添加试题"]'
       self.wait_for_element_visible(dialog_selector, timeout=10) 
       
       self.click(f'{dialog_selector}//label[text()="试题类型"]/following-sibling::div')
       self.sleep(1)
               # 定位"试题类型"标签后的el-select__wrapper元素
       wrapper_selector = f'{dialog_selector}//label[text()="试题类型"]/following-sibling::div//div//div'

       self.wait_for_element_visible(wrapper_selector)
       

        # 等待元素可见并获取属性值
       self.wait_for_element_visible(wrapper_selector, timeout=10)


       form_item_element = self.find_element(wrapper_selector)
       #打印元素html
       #print(f'内容为：{form_item_element.get_attribute('outerHTML')}')

       aria_controls = form_item_element.get_attribute('aria-describedby')

       print(f"aria-describedby属性值为: {aria_controls}")
       # 使用XPath直接定位第二个li元素
       second_li_selector = f'//div[@id="{aria_controls}"]//ul/li[1]'
       self.click(second_li_selector)

       # 生成随机字符串=时间戳+随机10位字符串
      
       # 获取当前时间戳
       timestamp = int(time.time())
       # 生成10位随机字符串
       random_str = ''.join(random.choices(string.ascii_letters + string.digits, k=10))
       # 合并时间戳和随机字符串
       random_str_with_timestamp = str(timestamp) + random_str
       print(random_str_with_timestamp)

       self.type(f'{dialog_selector}//label[text()="题干"]/following-sibling::div//textarea', random_str_with_timestamp)
       
      # self.click(f'{dialog_selector}//div[text()="选项"]/following-sibling::div//button[last()]')
       form_item_element = self.find_element(f'{dialog_selector}//div[text()="选项"]/following-sibling::div//button[last()]')
       form_item_element.click()
       self.sleep(0.1)
       form_item_element.click()
       self.sleep(0.1)
       form_item_element.click()
       self.sleep(0.1)
       form_item_element.click()
       self.sleep(0.1)

      
       textareas = self.find_elements(f'{dialog_selector}//div[text()="选项"]/following-sibling::div//textarea')
       # 为每个textarea赋值
       for i in range(4):
        if i < len(textareas):
            textareas[i].clear()  # 清空原有内容
            textareas[i].send_keys(f'选项{i}')
            print(f"已为第 {i+1} 个textarea赋值: {i}")
        else:
            print(f"警告: 值列表长度超过textarea数量({len(textareas)})")

       self.click(f'{dialog_selector}//label[text()="正确答案"]/following-sibling::div//label[last()]') 
       self.type(f'{dialog_selector}//label[text()="分值"]/following-sibling::div//input', "60")

       #******标签******
       self.click(f'{dialog_selector}//label[text()="标签"]/following-sibling::div')
       tagId = self.find_element(f'{dialog_selector}//label[text()="标签"]/following-sibling::div//div//div').get_attribute('aria-describedby')
       self.click(f'//div[@id="{tagId}"]//li[1]')
       self.type(f'{dialog_selector}//label[text()="提示参考"]/following-sibling::div//input', "提示参考1")
       self.type(f'{dialog_selector}//label[text()="描述"]/following-sibling::div//input', "描述1")
       self.type(f'{dialog_selector}//label[text()="解析"]/following-sibling::div//textarea', "解析1")
       self.click('button:contains("确 定")')
       

       self.type("input[placeholder='请输入题干']", random_str_with_timestamp)
       self.click('button:contains("搜索")')
       self.assert_text('共 1 条')
       self.save_screenshot_to_logs()
  
